package com.cinc.ebc.fi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cinc.ebc.fi.constant.*;
import com.cinc.ebc.fi.entity.*;
import com.cinc.ebc.fi.mapper.*;
import com.cinc.ebc.fi.model.FiCustomerAdvanceReceiveDetailModel;
import com.cinc.ebc.fi.model.FiCustomerReceiveDetailModel;
import com.cinc.ebc.fi.model.FiReceiveVoucherDetailModel;
import com.cinc.ebc.fi.service.IFiReceiveVoucherService;
import com.cinc.ebc.fi.vo.FiCustomerReceiveDetailPage;
import com.cinc.ebc.fi.vo.FiReceiveVoucherDetailPage;
import com.cinc.ebc.mm.constant.StockInOutDetailVerifyStatus;
import com.cinc.ebc.mm.entity.MmStockInOutDetail;
import com.cinc.ebc.mm.mapper.MmStockInOutDetailMapper;
import com.cinc.mdm.modules.codeRule.service.IMdmCodeRuleService;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * @Description: 收款单
 * @Author: jeecg-boot
 * @Date:   2022-12-17
 * @Version: V1.0
 */
@Service
public class FiReceiveVoucherServiceImpl extends ServiceImpl<FiReceiveVoucherMapper, FiReceiveVoucher> implements IFiReceiveVoucherService {

	@Autowired
	private FiReceiveVoucherMapper fiReceiveVoucherMapper;
	@Autowired
	private FiReceiveVoucherDetailMapper fiReceiveVoucherDetailMapper;

	@Autowired
	private IMdmCodeRuleService mdmCodeRuleService;

	@Autowired
	private MmStockInOutDetailMapper mmStockInOutDetailMapper;

	@Autowired
	private FiArOriginalAmountMapper fiArOriginalAmountMapper;

	@Autowired
	private FiOtherReceiveDetailMapper fiOtherReceiveDetailMapper;

	@Autowired
	private FiOtherReceiveMapper fiOtherReceiveMapper;

	@Override
	@Transactional
	public void saveMain(FiReceiveVoucher fiReceiveVoucher, List<FiReceiveVoucherDetail> fiReceiveVoucherDetailList) {
		BigDecimal actAmount = fiReceiveVoucher.getActAmount() == null ? BigDecimal.ZERO : fiReceiveVoucher.getActAmount();
		BigDecimal verifyAmount = fiReceiveVoucher.getVerifyAmount() == null ? BigDecimal.ZERO : fiReceiveVoucher.getVerifyAmount();

		if(ReceiveVoucherType.TYPE_01.equals(fiReceiveVoucher.getVoucherType())){
			if(actAmount.compareTo(verifyAmount) == -1){
				throw new RuntimeException("实收金额不能小于核销金额");
			}
			// 预收款处理
			BigDecimal preAmount = actAmount.subtract(verifyAmount);
			if(preAmount.compareTo(BigDecimal.ZERO)== 1){
				fiReceiveVoucher.setActAmount(verifyAmount);
				// 实收金额超出部分，转为预收款
				FiReceiveVoucher fiReceiveVoucherPre = new FiReceiveVoucher();
				fiReceiveVoucherPre.setVoucherCode(mdmCodeRuleService.generateFiReceiveVoucherCode());
				fiReceiveVoucherPre.setVoucherDate(fiReceiveVoucher.getVoucherDate());
				fiReceiveVoucherPre.setVoucherType(ReceiveVoucherType.TYPE_02);
				fiReceiveVoucherPre.setCurrency(fiReceiveVoucher.getCurrency());
				fiReceiveVoucherPre.setCustomerId(fiReceiveVoucher.getCustomerId());
				fiReceiveVoucherPre.setSettleAccountId(fiReceiveVoucher.getSettleAccountId());
				fiReceiveVoucherPre.setActAmount(preAmount);
				fiReceiveVoucherPre.setStatus(AparVoucherStatus.STATUS_01);
				fiReceiveVoucherPre.setMaker(fiReceiveVoucher.getMaker());
				fiReceiveVoucherPre.setUnverifyAmount(preAmount);
				fiReceiveVoucherMapper.insert(fiReceiveVoucherPre);
			}
		}else{
			if(verifyAmount.compareTo(BigDecimal.ZERO) != 0 && actAmount.compareTo(verifyAmount) != 0){
				throw new RuntimeException("实收金额与核销金额不符，请修改");
			}
		}
		fiReceiveVoucher.setUnverifyAmount(fiReceiveVoucher.getActAmount().subtract(fiReceiveVoucher.getVerifyAmount()));
		fiReceiveVoucherMapper.insert(fiReceiveVoucher);
		//
		if(fiReceiveVoucherDetailList!=null && fiReceiveVoucherDetailList.size()>0) {
			for(FiReceiveVoucherDetail entity:fiReceiveVoucherDetailList) {
				//外键设置
				entity.setMainId(fiReceiveVoucher.getId());
				BigDecimal detailVerifyAmount = entity.getVerifyAmount() == null ? BigDecimal.ZERO : entity.getVerifyAmount();
				if(detailVerifyAmount.compareTo(BigDecimal.ZERO) == -1){
					throw new RuntimeException("核销金额不能小于零");
				}
				// 业务单据处理
				handleVoucherDetail(entity);
				// 新增
				fiReceiveVoucherDetailMapper.insert(entity);
			}
		}
	}

	/**
	 * 处理业务单据
	 * @param entity
	 */
	private void handleVoucherDetail(FiReceiveVoucherDetail entity){
		FiReceiveVoucherDetail entityHis = fiReceiveVoucherDetailMapper.selectById(entity.getId());
		if(AparVoucherDetailBillType.AR_01.equals(entity.getBillType())) {
			//销售出库单
			MmStockInOutDetail stockInOutDetail = mmStockInOutDetailMapper.selectById(entity.getBillId());
			if (stockInOutDetail.getVerifyStatus().equals(StockInOutDetailVerifyStatus.STATUS_03)) {
				throw new RuntimeException("已核销单据，不能收款");
			}
			BigDecimal sumPrice = stockInOutDetail.getSumPrice() == null ? BigDecimal.ZERO : stockInOutDetail.getSumPrice();
			BigDecimal stockVerifyAmount = stockInOutDetail.getVerifyAmount() == null ? BigDecimal.ZERO : stockInOutDetail.getVerifyAmount();
			BigDecimal verifyAmount = entity.getVerifyAmount() == null ? BigDecimal.ZERO : entity.getVerifyAmount();
			BigDecimal finalAmount = BigDecimal.ZERO;
			if (entityHis == null) {
				finalAmount = verifyAmount.add(stockVerifyAmount);
			} else {
				BigDecimal verifyAmountHis = entityHis.getVerifyAmount() == null ? BigDecimal.ZERO : entityHis.getVerifyAmount();
				finalAmount = stockVerifyAmount.subtract(verifyAmountHis).add(verifyAmount);
			}
			if (finalAmount.compareTo(sumPrice) == 1) {
				throw new RuntimeException("核销金额不能大于单据金额");
			}
			MmStockInOutDetail stockInOutDetailModify = new MmStockInOutDetail();
			stockInOutDetailModify.setId(entity.getBillId());
			stockInOutDetailModify.setVerifyDate(new Date());
			stockInOutDetailModify.setVerifyAmount(finalAmount);
			mmStockInOutDetailMapper.updateById(stockInOutDetailModify);
		}else if(AparVoucherDetailBillType.AR_04.equals(entity.getBillType())) {
			// 应收期初余额
			FiArOriginalAmount fiArOriginalAmount = fiArOriginalAmountMapper.selectById(entity.getBillId());
			if (fiArOriginalAmount.getSettleStatus().equals(AparSettleStatus.STATUS_30)) {
				throw new RuntimeException("期初已结算完成，不能付款");
			}
			BigDecimal initialBalance = fiArOriginalAmount.getInitialBalance() == null ? BigDecimal.ZERO : fiArOriginalAmount.getInitialBalance();
			BigDecimal originalVerifyAmount = fiArOriginalAmount.getVerifyAmount() == null ? BigDecimal.ZERO : fiArOriginalAmount.getVerifyAmount();
			BigDecimal verifyAmount = entity.getVerifyAmount() == null ? BigDecimal.ZERO : entity.getVerifyAmount();
			BigDecimal finalAmount = BigDecimal.ZERO;
			if (entityHis == null) {
				finalAmount = verifyAmount.add(originalVerifyAmount);
			} else {
				BigDecimal verifyAmountHis = entityHis.getVerifyAmount() == null ? BigDecimal.ZERO : entityHis.getVerifyAmount();
				finalAmount = originalVerifyAmount.subtract(verifyAmountHis).add(verifyAmount);
			}
			if (finalAmount.compareTo(initialBalance) == 1) {
				throw new RuntimeException("核销金额不能大于期初余额");
			}
			FiArOriginalAmount fiArOriginalAmountModify = new FiArOriginalAmount();
			fiArOriginalAmountModify.setId(entity.getBillId());
			fiArOriginalAmountModify.setVerifyAmount(finalAmount);
			fiArOriginalAmountMapper.updateById(fiArOriginalAmountModify);
		}else if(AparVoucherDetailBillType.AR_02.equals(entity.getBillType())) {
			// 其他应收单
			FiOtherReceiveDetail fiOtherReceiveDetail = fiOtherReceiveDetailMapper.selectById(entity.getBillId());
			if (fiOtherReceiveDetail.getStatus().equals(AparOtherDetailStatus.STATUS_03)) {
				throw new RuntimeException("已完成单据，不能付款");
			}
			BigDecimal otherReceiveAmount = fiOtherReceiveDetail.getReceiveAmount() == null ? BigDecimal.ZERO : fiOtherReceiveDetail.getReceiveAmount();
			BigDecimal otherVerifyAmount = fiOtherReceiveDetail.getVerifyAmount() == null ? BigDecimal.ZERO : fiOtherReceiveDetail.getVerifyAmount();
			BigDecimal verifyAmount = entity.getVerifyAmount() == null ? BigDecimal.ZERO : entity.getVerifyAmount();
			BigDecimal finalAmount = BigDecimal.ZERO;
			if (entityHis == null) {
				finalAmount = verifyAmount.add(otherVerifyAmount);
			} else {
				BigDecimal verifyAmountHis = entityHis.getVerifyAmount() == null ? BigDecimal.ZERO : entityHis.getVerifyAmount();
				finalAmount = otherVerifyAmount.subtract(verifyAmountHis).add(verifyAmount);
			}
			if (finalAmount.compareTo(otherReceiveAmount) == 1) {
				throw new RuntimeException("核销金额不能大于单据金额");
			}
			FiOtherReceiveDetail fiOtherReceiveDetailModify = new FiOtherReceiveDetail();
			fiOtherReceiveDetailModify.setId(entity.getBillId());
			fiOtherReceiveDetailModify.setVerifyAmount(finalAmount);
			fiOtherReceiveDetailMapper.updateById(fiOtherReceiveDetailModify);
			// 更新主表核销金额
			fiOtherReceiveMapper.autoUpdateVerifyAmount(fiOtherReceiveDetail.getMainId());
		}
	}

	@Override
	@Transactional
	public void updateMain(FiReceiveVoucher fiReceiveVoucher,List<FiReceiveVoucherDetail> fiReceiveVoucherDetailList) {
		FiReceiveVoucher fiReceiveVoucherHis = fiReceiveVoucherMapper.selectById(fiReceiveVoucher.getId());
		if(AparVoucherStatus.STATUS_03.equals(fiReceiveVoucherHis.getStatus())){
			throw new RuntimeException("已经核销完成，不能修改");
		}
		BigDecimal mainActAmount = fiReceiveVoucher.getActAmount() == null ? BigDecimal.ZERO : fiReceiveVoucher.getActAmount();
		BigDecimal mainVerifyAmount = fiReceiveVoucher.getVerifyAmount() == null ? BigDecimal.ZERO : fiReceiveVoucher.getVerifyAmount();
		if(ReceiveVoucherType.TYPE_01.equals(fiReceiveVoucher.getVoucherType())){
			if(AparVoucherStatus.STATUS_02.equals(fiReceiveVoucherHis.getStatus())){
				throw new RuntimeException("已经审核，不能修改");
			}
			if(mainActAmount.compareTo(mainVerifyAmount) == -1){
				throw new RuntimeException("实收金额不能小于核销金额");
			}
			// 预收款处理
			BigDecimal preAmount = mainActAmount.subtract(mainVerifyAmount);
			if(preAmount.compareTo(BigDecimal.ZERO)== 1){
				fiReceiveVoucher.setActAmount(mainVerifyAmount);
				// 实收金额超出部分，转为预收款
				FiReceiveVoucher fiReceiveVoucherPre = new FiReceiveVoucher();
				fiReceiveVoucherPre.setVoucherCode(mdmCodeRuleService.generateFiReceiveVoucherCode());
				fiReceiveVoucherPre.setVoucherDate(fiReceiveVoucher.getVoucherDate());
				fiReceiveVoucherPre.setVoucherType(ReceiveVoucherType.TYPE_02);
				fiReceiveVoucherPre.setCurrency(fiReceiveVoucher.getCurrency());
				fiReceiveVoucherPre.setCustomerId(fiReceiveVoucher.getCustomerId());
				fiReceiveVoucherPre.setSettleAccountId(fiReceiveVoucher.getSettleAccountId());
				fiReceiveVoucherPre.setActAmount(preAmount);
				fiReceiveVoucherPre.setStatus(AparVoucherStatus.STATUS_01);
				fiReceiveVoucherPre.setMaker(fiReceiveVoucher.getMaker());
				fiReceiveVoucherPre.setUnverifyAmount(preAmount);
				fiReceiveVoucherMapper.insert(fiReceiveVoucherPre);
			}
		}else{
			BigDecimal mainActAmountHis = fiReceiveVoucherHis.getActAmount() == null ? BigDecimal.ZERO : fiReceiveVoucherHis.getActAmount();
			if(AparVoucherStatus.STATUS_02.equals(fiReceiveVoucherHis.getStatus()) && mainActAmount.compareTo(mainActAmountHis) != 0){
				throw new RuntimeException("已经审核，不能修改实付金额");
			}
			if(mainVerifyAmount.compareTo(BigDecimal.ZERO) != 0 && mainActAmount.compareTo(mainVerifyAmount) != 0){
				throw new RuntimeException("实收金额与核销金额不符，请修改");
			}
		}
		fiReceiveVoucher.setUnverifyAmount(fiReceiveVoucher.getActAmount().subtract(fiReceiveVoucher.getVerifyAmount()));
		fiReceiveVoucherMapper.updateById(fiReceiveVoucher);
		// 获取子表数据
		LambdaQueryWrapper<FiReceiveVoucherDetail> detailQuery = new LambdaQueryWrapper<>();
		detailQuery.eq(FiReceiveVoucherDetail::getMainId, fiReceiveVoucher.getId());
		List<FiReceiveVoucherDetail> receiveVoucherDetailHisList = fiReceiveVoucherDetailMapper.selectList(detailQuery);
		List<String> detailIdList = receiveVoucherDetailHisList.stream().map(FiReceiveVoucherDetail::getId).collect(Collectors.toList());
		List<String> delIdList = detailIdList;
		// 子表数据更新
		if(fiReceiveVoucherDetailList!=null && fiReceiveVoucherDetailList.size()>0) {
			for(FiReceiveVoucherDetail entity:fiReceiveVoucherDetailList) {
				BigDecimal verifyAmount = entity.getVerifyAmount() == null ? BigDecimal.ZERO : entity.getVerifyAmount();
				if(verifyAmount.compareTo(BigDecimal.ZERO) == -1){
					throw new RuntimeException("核销金额不能小于零");
				}
				entity.setMainId(fiReceiveVoucher.getId());
				//业务单据处理
				handleVoucherDetail(entity);
				//
				if(!detailIdList.contains(entity.getId())){
					//新增
					fiReceiveVoucherDetailMapper.insert(entity);
				}else{
					//修改
					fiReceiveVoucherDetailMapper.updateById(entity);
				}
			}
			//获取删除记录ID
			List<String> curIdList = fiReceiveVoucherDetailList.stream().map(FiReceiveVoucherDetail::getId).collect(Collectors.toList());
			delIdList = detailIdList.stream().filter(id -> !curIdList.contains(id)).collect(Collectors.toList());
		}
		// 删除记录处理
		if(oConvertUtils.listIsNotEmpty(delIdList)){
			for(String id : delIdList){
				// 业务单据处理
				FiReceiveVoucherDetail entityHis = fiReceiveVoucherDetailMapper.selectById(id);
				FiReceiveVoucherDetail entityDel = new FiReceiveVoucherDetail();
				entityDel.setId(id);
				entityDel.setBillId(entityHis.getBillId());
				entityDel.setBillType(entityHis.getBillType());
				entityDel.setVerifyAmount(BigDecimal.ZERO);
				handleVoucherDetail(entityDel);
				// 删除
				fiReceiveVoucherDetailMapper.deleteById(id);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		FiReceiveVoucher fiReceiveVoucher = fiReceiveVoucherMapper.selectById(id);
		if(!AparVoucherStatus.STATUS_01.equals(fiReceiveVoucher.getStatus())){
			throw new RuntimeException("已经审核，不能删除");
		}
		List<FiReceiveVoucherDetail> fiReceiveVoucherDetailList = fiReceiveVoucherDetailMapper.selectByMainId(id);
		if(oConvertUtils.listIsNotEmpty(fiReceiveVoucherDetailList)){
			for(FiReceiveVoucherDetail fiReceiveVoucherDetail: fiReceiveVoucherDetailList){
				//处理业务单据
				FiReceiveVoucherDetail detailDel = new FiReceiveVoucherDetail();
				detailDel.setId(fiReceiveVoucherDetail.getId());
				detailDel.setBillId(fiReceiveVoucherDetail.getBillId());
				detailDel.setBillType(fiReceiveVoucherDetail.getBillType());
				detailDel.setVerifyAmount(BigDecimal.ZERO);
				handleVoucherDetail(detailDel);
			}
		}
		//
		fiReceiveVoucherDetailMapper.deleteByMainId(id);
		fiReceiveVoucherMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			delMain(id.toString());
		}
	}

	@Override
	@Transactional
	public void audit(List<FiReceiveVoucher> fiReceiveVoucherList) {
		for(FiReceiveVoucher fiReceiveVoucher : fiReceiveVoucherList){
			FiReceiveVoucher fiReceiveVoucherEntity = fiReceiveVoucherMapper.selectById(fiReceiveVoucher.getId());
			if(fiReceiveVoucherEntity==null) {
				throw new RuntimeException("未找到对应数据");
			}
			String status = fiReceiveVoucherEntity.getStatus();
			if(!AparVoucherStatus.STATUS_01.equals(status)){
				throw new RuntimeException("已经审核，请不要重复操作");
			}
			BigDecimal verifyAmount = fiReceiveVoucherEntity.getVerifyAmount();
			if(ReceiveVoucherType.TYPE_01.equals(fiReceiveVoucherEntity.getVoucherType())
					&& (verifyAmount == null || BigDecimal.ZERO.compareTo(verifyAmount) == 0)){
				throw new RuntimeException("尚未进行核销，不能审核");
			}
			BigDecimal actAmount = fiReceiveVoucherEntity.getActAmount();
			if(actAmount == null || BigDecimal.ZERO.compareTo(actAmount) == 0 || actAmount.compareTo(verifyAmount) == -1){
				throw new RuntimeException("实收金额有误，请修改数据");
			}
			// 审核
			FiReceiveVoucher fiReceiveVoucherAudit = new FiReceiveVoucher();
			fiReceiveVoucherAudit.setId(fiReceiveVoucher.getId());
			fiReceiveVoucherAudit.setAuditDate(new Date());
			fiReceiveVoucherAudit.setAuditFlag("1");
			LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
			fiReceiveVoucherAudit.setAuditor(sysUser != null ? sysUser.getUsername(): null);
			if(ReceiveVoucherType.TYPE_01.equals(fiReceiveVoucherEntity.getVoucherType())){
				// 普通收款，审核同时核销
				fiReceiveVoucherAudit.setVerifyFlag("1");
				fiReceiveVoucherAudit.setVerifyDate(new Date());
				fiReceiveVoucherAudit.setVerifier(sysUser != null ? sysUser.getUsername(): null);
				fiReceiveVoucherAudit.setStatus(AparVoucherStatus.STATUS_03);
				fiReceiveVoucherAudit.setActAmount(verifyAmount);
			}else{
				// 预收款、直接收款款，只审核
				fiReceiveVoucherAudit.setStatus(AparVoucherStatus.STATUS_02);
			}
			fiReceiveVoucherMapper.updateById(fiReceiveVoucherAudit);
			// 更新业务单据状态
			List<FiReceiveVoucherDetail> fiReceiveVoucherDetailList = fiReceiveVoucherDetailMapper.selectByMainId(fiReceiveVoucher.getId());
			for (FiReceiveVoucherDetail fiReceiveVoucherDetail : fiReceiveVoucherDetailList) {
				handleVoucherDetailStatus(fiReceiveVoucherDetail);
			}
		}
	}

	@Transactional
	@Override
	public void verify(List<FiReceiveVoucher> fiReceiveVoucherList) {
		for(FiReceiveVoucher fiReceiveVoucher : fiReceiveVoucherList){
			FiReceiveVoucher fiReceiveVoucherEntity = fiReceiveVoucherMapper.selectById(fiReceiveVoucher.getId());
			if(fiReceiveVoucherEntity==null) {
				throw new RuntimeException("未找到对应数据");
			}
			String status = fiReceiveVoucherEntity.getStatus();
			if(!AparVoucherStatus.STATUS_02.equals(status)){
				throw new RuntimeException("当前状态不能进行核销");
			}
			BigDecimal verifyAmount = fiReceiveVoucherEntity.getVerifyAmount();
			if (verifyAmount == null || BigDecimal.ZERO.compareTo(verifyAmount) == 0) {
				throw new RuntimeException("请先维护核销金额");
			}
			BigDecimal actAmount = fiReceiveVoucherEntity.getActAmount();
			if (actAmount == null || BigDecimal.ZERO.compareTo(actAmount) == 0 || actAmount.compareTo(verifyAmount) != 1) {
				throw new RuntimeException("实付金额有误，请修改数据");
			}
			// 核销
			FiReceiveVoucher fiReceiveVoucherAudit = new FiReceiveVoucher();
			fiReceiveVoucherAudit.setVerifyFlag("1");
			fiReceiveVoucherAudit.setVerifyDate(new Date());
			LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
			fiReceiveVoucherAudit.setVerifier(sysUser != null ? sysUser.getUsername(): null);
			fiReceiveVoucherAudit.setStatus(AparVoucherStatus.STATUS_03);
			fiReceiveVoucherMapper.updateById(fiReceiveVoucherAudit);
			// 更新业务单据状态
			List<FiReceiveVoucherDetail> fiReceiveVoucherDetailList = fiReceiveVoucherDetailMapper.selectByMainId(fiReceiveVoucher.getId());
			for (FiReceiveVoucherDetail fiReceiveVoucherDetail : fiReceiveVoucherDetailList) {
				handleVoucherDetailStatus(fiReceiveVoucherDetail);
			}
		}
	}

	/**
	 * 明细分页查询
	 *
	 * @param page
	 * @param param
	 * @return
	 */
	@Override
	public void detailPage(Page<FiReceiveVoucherDetailModel> page, FiReceiveVoucherDetailPage param) {
		page.setRecords(fiReceiveVoucherDetailMapper.selectDetailList(page, param, null));
	}

	/**
	 * 客户应收明细账-收款
	 *
	 * @param page
	 * @param param
	 * @return
	 */
	@Override
	public void customerReceiveDetailPage(Page<FiCustomerReceiveDetailModel> page, FiCustomerReceiveDetailPage param) {
		page.setRecords(fiReceiveVoucherDetailMapper.selectCustomerReceiveList(page, param, null));
	}

	/**
	 * 客户应收明细账-预收款
	 *
	 * @param page
	 * @param param
	 * @return
	 */
	@Override
	public void customerAdvanceReceiveDetailPage(Page<FiCustomerAdvanceReceiveDetailModel> page, FiCustomerReceiveDetailPage param) {
		page.setRecords(fiReceiveVoucherDetailMapper.selectCustomerAdvanceReceiveList(page, param, null));
	}

	private void handleVoucherDetailStatus (FiReceiveVoucherDetail entity){
		if(AparVoucherDetailBillType.AR_01.equals(entity.getBillType())) {
			//销售出库单
			MmStockInOutDetail stockInOutDetail = mmStockInOutDetailMapper.selectById(entity.getBillId());
			BigDecimal sumPrice = stockInOutDetail.getSumPrice() == null ? BigDecimal.ZERO : stockInOutDetail.getSumPrice();
			BigDecimal stockVerifyAmount = stockInOutDetail.getVerifyAmount() == null ? BigDecimal.ZERO : stockInOutDetail.getVerifyAmount();
			if(stockVerifyAmount.compareTo(sumPrice) == 1){
				throw new RuntimeException("核销金额不能大于单据金额");
			}
			MmStockInOutDetail stockInOutDetailModify = new MmStockInOutDetail();
			stockInOutDetailModify.setId(stockInOutDetail.getId());
			if (stockVerifyAmount.compareTo(sumPrice) == 0) {
				stockInOutDetailModify.setVerifyStatus(StockInOutDetailVerifyStatus.STATUS_03);
			} else if (stockVerifyAmount.compareTo(BigDecimal.ZERO) == 1) {
				stockInOutDetailModify.setVerifyStatus(StockInOutDetailVerifyStatus.STATUS_02);
			} else {
				stockInOutDetailModify.setVerifyStatus(StockInOutDetailVerifyStatus.STATUS_01);
			}
			mmStockInOutDetailMapper.updateById(stockInOutDetailModify);
		}else if(AparVoucherDetailBillType.AR_04.equals(entity.getBillType())) {
			// 应收期初余额
			FiArOriginalAmount fiArOriginalAmount = fiArOriginalAmountMapper.selectById(entity.getBillId());
			BigDecimal initialBalance = fiArOriginalAmount.getInitialBalance() == null ? BigDecimal.ZERO : fiArOriginalAmount.getInitialBalance();
			BigDecimal originalVerifyAmount = fiArOriginalAmount.getVerifyAmount() == null ? BigDecimal.ZERO : fiArOriginalAmount.getVerifyAmount();
			if(originalVerifyAmount.compareTo(initialBalance) == 1){
				throw new RuntimeException("核销金额不能大于期初余额");
			}
			FiArOriginalAmount fiArOriginalAmountModify = new FiArOriginalAmount();
			fiArOriginalAmountModify.setId(fiArOriginalAmount.getId());
			if (originalVerifyAmount.compareTo(initialBalance) == 0) {
				fiArOriginalAmountModify.setSettleStatus(AparSettleStatus.STATUS_30);
			} else if (originalVerifyAmount.compareTo(BigDecimal.ZERO) == 1) {
				fiArOriginalAmountModify.setSettleStatus(AparSettleStatus.STATUS_20);
			} else {
				fiArOriginalAmountModify.setSettleStatus(AparSettleStatus.STATUS_10);
			}
			fiArOriginalAmountMapper.updateById(fiArOriginalAmountModify);
		}else if(AparVoucherDetailBillType.AR_02.equals(entity.getBillType())) {
			// 其他应收单
			FiOtherReceiveDetail fiOtherReceiveDetail = fiOtherReceiveDetailMapper.selectById(entity.getBillId());
			BigDecimal otherReceiveAmount = fiOtherReceiveDetail.getReceiveAmount() == null ? BigDecimal.ZERO : fiOtherReceiveDetail.getReceiveAmount();
			BigDecimal otherVerifyAmount = fiOtherReceiveDetail.getVerifyAmount() == null ? BigDecimal.ZERO : fiOtherReceiveDetail.getVerifyAmount();
			if(otherVerifyAmount.compareTo(otherReceiveAmount) == 1){
				throw new RuntimeException("核销金额不能大于单据金额");
			}
			FiOtherReceiveDetail fiOtherReceiveDetailModify = new FiOtherReceiveDetail();
			fiOtherReceiveDetailModify.setId(fiOtherReceiveDetail.getId());
			if (otherVerifyAmount.compareTo(otherReceiveAmount) == 0) {
				fiOtherReceiveDetailModify.setStatus(AparOtherDetailStatus.STATUS_03);
			} else if (otherVerifyAmount.compareTo(BigDecimal.ZERO) == 1) {
				fiOtherReceiveDetailModify.setStatus(AparOtherDetailStatus.STATUS_02);
			} else {
				fiOtherReceiveDetailModify.setStatus(AparOtherDetailStatus.STATUS_01);
			}
			fiOtherReceiveDetailMapper.updateById(fiOtherReceiveDetailModify);
			// 更新主表状态
			fiOtherReceiveMapper.autoUpdateStatus(fiOtherReceiveDetail.getMainId());
		}
	}
}
